#include "HealthManager.h"
#include "FuriousTourist.h"

HealthManager::HealthManager(void)
{
}


HealthManager::~HealthManager(void)
{
}

HealthManager::HealthManager(const HealthManager &hm)
{
}

HealthManager* HealthManager::operator=(const HealthManager &hm)
{
	return this;
}

HealthManager* HealthManager::Instance()
{
	static HealthManager instance;

	return &instance;
}

// checks if the passed object took a hit
// during the last iteration, and if so, returns how
// much damage it took
int HealthManager::GetDamage(physics::RigidBody* object)
{
	collisions::iterator i = mCollisions.begin();

	int damage = 0;

	while(i != mCollisions.end())
	{
		if ((*i)->object == object)
		{
			// found the object. Keep track of how much damage it took.
			damage += (*i)->damage;
			delete *i;
			i = mCollisions.erase(i);
		} 
		else
		{
			// otherwise check the next one
			i++;
		}
	}
	// object could take multiple hits, so its wise
	// to go through the entire list first
	return damage; 
}

// attempts the rigid body to the list of objects
// that need to be considered. If it is a worthwhile
// collision, it will add it to the std::vector
void HealthManager::AddCollision(physics::RigidBody* object, physics::real force)
{
	int damage = force * DAMAGE_SCALE_FACTOR;
	if ( damage >= DAMAGE_MINIMUM)
	{
		GameObjectCollision* c = new GameObjectCollision;
		c->damage = damage;
		c->object = object;
		mCollisions.push_back(c);
	}

	if ( damage >= DAMAGE_SOUNDMINIMUM)
	{
		switch(object->GetMaterial())
		{
		case OBJECTMATERIAL_WOOD:
			SOUNDMANAGER->SoundPlay(SOUND_WOODIMPACT);
			break;

		case OBJECTMATERIAL_STONE:
			SOUNDMANAGER->SoundPlay(SOUND_STONEIMPACT);
			break;

		case OBJECTMATERIAL_COCONUT:
			SOUNDMANAGER->SoundPlay(SOUND_COCONUTIMPACT);
		}
	}
}

// clears all entries, just so we don't waste memory
// should be called at the end of the iteration
void HealthManager::ClearAll(void)
{
	collisions::iterator i = mCollisions.begin();

	while(i != mCollisions.end())
	{
		// found the object. Keep track of how much damage it took.
		delete *i;
		i = mCollisions.erase(i);
	}
}